package com.atguigu.spring.controller;

import com.atguigu.spring.service.DemoService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

public class DemoController implements InitializingBean, DisposableBean {

    public DemoController() {
        System.out.println("★★★★★DemoController类创建对象了★★★★★");
    }

    // -------------------初始化阶段的三个生命周期方法-------------------
    @PostConstruct
    public void initMethodByPostConstruct() {
        System.out.println("★★★★★[初始化方法]基于@PostConstruct注解★★★★★");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("★★★★★[初始化方法]基于InitializingBean接口★★★★★");
    }

    // 需要配置：@Bean(initMethod = "initMethodByBean")
    public void initMethodByBean() {
        System.out.println("★★★★★[初始化方法]基于@Bean注解★★★★★");
    }
    // -------------------销毁阶段的三个生命周期方法-------------------
    @PreDestroy
    public void destroyMethodByPreDestroy() {
        System.out.println("★★★★★[销毁方法]基于@PreDestroy注解★★★★★");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("★★★★★[销毁方法]基于DisposableBean接口★★★★★");
    }

    public void destroyMethodByBean() {
        System.out.println("★★★★★[销毁方法]基于@Bean注解★★★★★");
    }

    // -------------------------------------------------------------

    private DemoService demoService;

    @Autowired
    public void setDemoService(DemoService demoService) {
        this.demoService = demoService;
        System.out.println("★★★★★DemoController执行装配★★★★★");
    }
}
